//
//  GoodsAttributeRelation.swift
//  ReactNativeIOS
//
//  Created by FishWithoutWings on 2017/12/16.
//  Copyright © 2017年 chenliang. All rights reserved.
//

import RealmSwift

class GoodsAttributeRelation: Object {
    
    @objc dynamic var id = 0
    @objc dynamic var catId = 0
    @objc dynamic var attrId = 0
    @objc dynamic var sortNo = 0
    
    override class func primaryKey() -> String? {
        return "id"
    }
    
    class func create(_ goodsAttributeRelations: [Any], isAsync: Bool = true) {
        let closure = {
            let realm = YYRealm.initialize()
            
            realm.beginWrite()
            realm.delete(realm.objects(self))
            for goodsAttributeRelation in goodsAttributeRelations {
                realm.create(self, value: goodsAttributeRelation, update: true)
            }
            try! realm.commitWrite()
        }
        
        if isAsync {
            YYRealm.async(closure)
        } else {
            closure()
        }
    }
    
    class func getAll(catId: Int) -> [GoodsAttributeRelation] {
        let realm = YYRealm.initialize()
        return realm.objects(self).filter("catId = \(catId)").map({ (goodsAttributeRelation) -> GoodsAttributeRelation in
            return goodsAttributeRelation
        })
        
    }

}
